from django.db import models

# Create your models here.

# 基类
class BaseModel(models.Model):
    create_date = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta:
        abstract = True

# 学校表
class SchoolModel(BaseModel):
    name = models.CharField('学校名称', max_length=30)

    class Meta:
        db_table = 'school_tb'
        verbose_name = '学校表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 班级表
class ClbumModel(BaseModel):
    name = models.CharField('班级名称', max_length=20)
    school = models.ForeignKey(SchoolModel, on_delete=models.CASCADE, verbose_name='所属学校')

    class Meta:
        db_table = 'clbum_tb'
        verbose_name = '班级表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 角色表
class RoleModel(BaseModel):

    name = models.CharField('角色', max_length=20)

    class Meta:
        db_table = 'role_tb'
        verbose_name = '角色表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 权限表
class PermissionModel(BaseModel):

    name = models.CharField('权限名称', max_length=20)
    permission_url = models.CharField('权限路由', max_length=128)

    class Meta:
        db_table = 'permission_tb'
        verbose_name = '权限表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 用户表
class UserModel(BaseModel):

    account = models.CharField('账号', max_length=20)
    password = models.CharField('密码', max_length=256)
    teacher = models.IntegerField(verbose_name='所属老师', null=True, default=None)
    role = models.ForeignKey(RoleModel, on_delete=models.PROTECT, verbose_name="所属角色")
    clbum = models.ForeignKey(ClbumModel, on_delete=models.CASCADE, verbose_name='所属班级')

    class Meta:
        db_table = 'user_tb'
        verbose_name = '用户表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.account} 属于 {self.clbum.name}'

# 用户信息表
class UserinfoModel(BaseModel):

    TYPE = (
        (1, '男'),
        (2, '女'),
    )

    name = models.CharField('姓名', max_length=20)
    signature = models.CharField('个性签名', max_length=128, null=True, default='该用户很懒，未设置签名')
    avatar_url = models.CharField('头像', max_length=256, null=True, blank=True)
    sex = models.SmallIntegerField('性别', choices=TYPE, default=1)
    mobile = models.CharField('联系方式', max_length=11)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='所属用户')

    class Meta:
        db_table = 'userinfo_tb'
        verbose_name = '用户信息表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 权限角色中间表
class PermissionRoleModel(BaseModel):
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name="所属角色")
    permission = models.ForeignKey(PermissionModel, on_delete=models.CASCADE, verbose_name="所属权限")

    class Meta:
        db_table = "permission_role_tb"
        verbose_name_plural = "权限角色表"

    def __str__(self):
        return "%s角色的权限为%s"%(self.role.name, self.permission.name)

# 问候语表
class GreetingsModel(BaseModel):

    content = models.CharField('问候内容', max_length=128)
    start_time = models.IntegerField(verbose_name='开始时间')
    end_time = models.IntegerField(verbose_name='结束时间')

    class Meta:
        db_table = 'greetings_tb'
        verbose_name = '问候语表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.content

# 节日问候语表
class FesGreetingsModel(BaseModel):

    TYPE = (
        (1, '阳历'),
        (2, '阴历')
    )

    content = models.CharField('问候内容', max_length=256)
    holiday_name = models.CharField('节日名称', max_length=10)
    holiday_date = models.CharField('节日日期', max_length=20)
    date_type = models.SmallIntegerField('日期类型',choices=TYPE, default=1)

    class Meta:
        db_table = 'fes_greetings_tb'
        verbose_name = '节日问候语表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.holiday_name} 在 {self.holiday_date}'

# 搜索记录表
class SearchModel(BaseModel):

    content = models.CharField('搜索内容', max_length=50)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='所属用户')

    class Meta:
        db_table = 'search_tb'
        verbose_name = '搜索记录表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.account} 搜索了 {self.content}'

# 消息表
class MessageModel(BaseModel):
    interaction = models.CharField("操作、互动、社交", max_length=50)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="操作人", related_name="操作人")
    users = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='接收人', related_name='接收人')

    class Meta:
        db_table = 'message_tb'
        verbose_name = '消息表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.account} 对 {self.users.account} 进行了 {self.interaction}'

# 菜单表
class MenuModel(BaseModel):

    name = models.CharField('菜单名称', max_length=20)
    menu_url = models.CharField('菜单路径', max_length=128)
    menu = models.IntegerField('上一级菜单', null=True, blank=True)

    class Meta:
        db_table = 'menu_tb'
        verbose_name = '菜单表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 角色菜单表
class RoleMenuModel(BaseModel):

    role = models.ForeignKey(RoleModel,on_delete=models.CASCADE, verbose_name='所属角色')
    menu = models.ForeignKey(MenuModel,on_delete=models.CASCADE, verbose_name='所属菜单')

    class Meta:
        db_table = 'role_menu_tb'
        verbose_name = '角色菜单表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.role.name} 拥有 {self.menu.name} 菜单'

# 标签表
class LabelModel(BaseModel):
    name = models.CharField("标签名", max_length=20)

    class Meta:
        db_table = "label_tb"
        verbose_name_plural = "标签表"

    def __str__(self):
        return self.name

# 分类表
class ClassIfyModel(BaseModel):
    name = models.CharField("名称", max_length=20)
    sequence = models.IntegerField(verbose_name='顺序')
    classify = models.IntegerField(verbose_name='上级分类', null=True)

    class Meta:
        db_table = "classify_tb"
        verbose_name_plural = "分类表"

    def __str__(self):
        return self.name

# 学习内容表
class ContentsModel(BaseModel):
    title = models.CharField("标题", max_length=50)
    content = models.TextField("内容")
    intro = models.CharField("简介", max_length=100)
    is_open = models.BooleanField("是否公开", default=False)
    classify = models.ForeignKey(ClassIfyModel, on_delete=models.CASCADE, verbose_name='所属分类')
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE,verbose_name='发布人')

    class Meta:
        db_table = "contents_tb"
        verbose_name_plural = "学习内容表"

    def __str__(self):
        return self.content

# 标签内容表
class Label_ContentModel(BaseModel):

    content = models.ForeignKey(ContentsModel, on_delete=models.CASCADE, verbose_name='所属内容')
    label = models.ForeignKey(LabelModel, on_delete=models.CASCADE, verbose_name='所属标签')

    class Meta:
        db_table = 'label_content_tb'
        verbose_name = '标签内容表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.content.content} 包含 {self.label.name}'

# 学习任务表
class TaskModel(BaseModel):

    clbum = models.ForeignKey(ClbumModel, on_delete=models.CASCADE, verbose_name='所属班级')
    content = models.ForeignKey(ContentsModel, on_delete=models.CASCADE, verbose_name='所属内容')

    class Meta:
        db_table = 'task_tb'
        verbose_name = '学习任务表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.clbum.name} 有 {self.content.content}'

# 学习记录表
class RecordModel(BaseModel):

    ORDER_STATE = (
        (1, '未完成'),
        (2, '作弊完成'),
        (3, '已完成'),
        (4, '费力完成'),
    )

    task = models.ForeignKey(TaskModel, on_delete=models.CASCADE, verbose_name='所属任务')
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='完成人')
    is_status = models.SmallIntegerField('状态', choices=ORDER_STATE, default=1)
    tokinaga = models.CharField(verbose_name='时长', max_length=50, null=True, default=None)
    start_time = models.DateTimeField('开始时间', null=True, default=None)
    finish_time = models.DateTimeField('完成时间', null=True, default=None)

    class Meta:
        db_table = 'record_tb'
        verbose_name = '学习记录表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.account} 费时 {self.tokinaga} {self.is_status}'

# 学习资源表
class ResourceModel(BaseModel):
    name = models.CharField("书名", max_length=50)
    depict = models.CharField("介绍", max_length=100)
    price = models.DecimalField("价格", max_digits=5, decimal_places=2)
    sales = models.IntegerField("销量", default=0)
    user = models.ForeignKey(UserModel, verbose_name="所属用户", on_delete=models.CASCADE)
    content = models.ForeignKey(ContentsModel, on_delete=models.CASCADE, verbose_name='包含内容', null=True, blank=True)

    class Meta:
        db_table = "resource_tb"
        verbose_name_plural = "学习资源表"

    def __str__(self):
        return self.name

# 订单表
class OrderModel(BaseModel):
    ORDER_STATE = (
        (0, "未支付"),
        (1, "已支付")
    )
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE)
    pay_state = models.SmallIntegerField("订单状态", choices=ORDER_STATE, default=0)
    pay_time = models.DateTimeField(verbose_name="支付时间", null=True, auto_now_add=True)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    total_amount = models.DecimalField(verbose_name="金额", max_digits=7, decimal_places=2)
    order_id = models.CharField(verbose_name="订单编号", max_length=256)
    # 支付方式
    pay_method = models.CharField("支付方式", max_length=10, choices=(("1", "支付宝"), ("2", "银联")), default="1")
    resource = models.ForeignKey(ResourceModel, on_delete=models.PROTECT, verbose_name='所属资源')

    class Meta:
        db_table = "order_tb"
        verbose_name = "订单表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "订单 %s "% (self.id)

# 弹幕表
class BarrageModel(BaseModel):
    name = models.CharField("弹幕内容", max_length=50)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="所属用户")
    like = models.IntegerField(verbose_name='点赞量', default=0)
    content = models.ForeignKey(ContentsModel, on_delete=models.CASCADE, verbose_name='所属内容')

    class Meta:
        db_table = "barrage_tb"
        verbose_name_plural = "弹幕表"

    def __str__(self):
        return self.name

# 收藏表
class CollectModel(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="所属用户")
    resource = models.ForeignKey(ResourceModel, on_delete=models.CASCADE, verbose_name="所属资源")

    class Meta:
        db_table = "collect_tb"
        verbose_name_plural = "收藏表"

    def __str__(self):
        return "%s收藏的资源为%s"% (self.user.account, self.resource.content)


